//
//  LGComposeController.m
//  微博
//
//  Created by MS on 15-12-24.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGComposeController.h"
#import "LGTextView.h"
#import "LGComposeToolBar.h"
#import "LGComposePhotosView.h"
#import "LGComposeTool.h"
#import "MBProgressHUD+MJ.h"
@interface LGComposeController ()<UITextViewDelegate,LGComposeToolBarDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic,weak) UIBarButtonItem * compose;
@property (nonatomic,weak) LGTextView * textView;
@property (nonatomic,weak) LGComposeToolBar * toolBar;
@property (nonatomic,weak) LGComposePhotosView * composePhotosView;
@property (nonatomic,strong)  NSMutableArray * photosArray;
@end

@implementation LGComposeController

-(NSMutableArray *)photosArray
{
    if (_photosArray==nil) {
        _photosArray = [[NSMutableArray alloc] init];
    }
    return _photosArray;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //设置导航栏
    [self setNavBar];
    
    //添加textView
    [self setUpTextView];
    
    //添加工具条
    [self setUpToolBar];
    
    
    //监听键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChang:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    //添加图片视图
    [self setUpComposePhotosView];
    
}


-(void)setUpComposePhotosView
{
    LGComposePhotosView * composePhotosView = [[LGComposePhotosView alloc] initWithFrame:CGRectMake(0, 80, self.view.width, self.view.height-80)];
    self.composePhotosView = composePhotosView;
    [self.textView addSubview:composePhotosView];
}

#pragma mark -键盘frame变化收到消息调用
-(void)keyboardFrameChang:(NSNotification *)note
{
    CGFloat durtion = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    if (frame.origin.y==self.view.height) {//未弹出
        
        [UIView animateWithDuration:durtion animations:^{
            self.toolBar.transform =CGAffineTransformIdentity; }];
    }
    else{//弹出
        [UIView animateWithDuration:durtion animations:^{
            self.toolBar.transform =CGAffineTransformMakeTranslation(0, -frame.size.height);
        }];
        
    }
}

-(void)setUpToolBar
{
    LGComposeToolBar * toolBar = [[LGComposeToolBar alloc] initWithFrame:CGRectMake(0, self.view.height -35, self.view.width, 35)];
    toolBar.delegate = self;
    self.toolBar = toolBar;
    [self.view addSubview:toolBar];
    
}

#pragma mark -点击工具条按钮调用
-(void)composeToolBar:(LGComposeToolBar *)toolBar didClickBtn:(NSInteger)index
{
    
    if (index==0) {
        
        //进入系统相册
        UIImagePickerController * pickVc = [[UIImagePickerController alloc] init];
        
        pickVc.delegate = self;
        pickVc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        
        [self presentViewController:pickVc animated:YES completion:nil];
        
    }
    
}

#pragma mark - 系统相册代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    UIImage * image = info[UIImagePickerControllerOriginalImage];
    [self.photosArray addObject:image];
    self.composePhotosView.image = image;
    _compose.enabled = YES;
    
}


-(void)setUpTextView
{
    LGTextView * textView = [[LGTextView alloc] initWithFrame:self.view.bounds];
    
    textView.alwaysBounceVertical = YES;
    textView.delegate = self;
    textView.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:textView];
    textView.placeHold = @"#分享新鲜事#...";
    self.textView = textView;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textState) name:UITextViewTextDidChangeNotification object:nil];
    
}

#pragma mark 输入文字改变时调用
-(void)textState
{
    if (self.textView.text.length) {
        self.textView.hiddenplaceHold = YES;
        _compose.enabled = YES;
    }
    else{
        
        self.textView.hiddenplaceHold = NO;
         _compose.enabled = NO;
    }
    
    
}


-(void)dealloc
{
 
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [_textView becomeFirstResponder];
    
}

-(void)setNavBar
{
    self.title = @"发微博";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(leftBtn)];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"发送" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(composeClick) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn sizeToFit];
    UIBarButtonItem * barItem =[[UIBarButtonItem alloc] initWithCustomView:btn];
    barItem.enabled = NO;
    self.compose = barItem;
    self.navigationItem.rightBarButtonItem = barItem;
}

-(void)leftBtn
{
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

-(void)composeClick
{
    
    if (self.photosArray.count) {
       
        [self sendImage];
    }
    else{
        
        [self sendtext];
    }
    
  
    
}

-(void)sendImage
{
    _compose.enabled = NO;
    
    NSString * str =_textView.text.length>0 ? _textView.text : @"分享图片";
    
    [LGComposeTool composeWithStatus:str image:self.photosArray[0] success:^{
        
        [MBProgressHUD showSuccess:@"发送成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
        _compose.enabled = YES;
        
    } failure:^(NSError *error) {
        [MBProgressHUD showSuccess:@"发送失败"];
         NSLog(@"%@",error);
        _compose.enabled = YES;

    }];
}

-(void)sendtext
{
    
    [LGComposeTool composeWithStatus:_textView.text success:^{
        
        [MBProgressHUD showSuccess:@"发送成功"];
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
